package com.mmall.common;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

/**
 * Created by qujian on 2017/6/9.
 */
public class TokenCache {

    private static Logger                       logger     = LoggerFactory
        .getLogger(TokenCache.class);

    public static final String TOKEN_PREFIX = "token_";


    /**
     * 静态内存块
     *initialCapacity：设置缓存的初始化容量
     * maximumSize：缓存的最大容量超过这个容量的时候，guava的cache会使用LRU算法（最少使用算法）来移除缓存项
     * expireAfterAccess：设置有效期
     */
    private static LoadingCache<String, String> localcache = CacheBuilder.newBuilder()
        .initialCapacity(1000).maximumSize(10000).expireAfterAccess(12, TimeUnit.HOURS)
        .build(new CacheLoader<String, String>() {
            //默认的数据加载实现，当调用get取值的时候，如果key没有对应的值，就调用这个方法进行加载
            @Override
            public String load(String s) throws Exception {
                return "null";
            }
        });

    public  static  void setKey(String key,String value){
        localcache.put(key,value);
    }

    public static String getKey(String key){
        String value = null;
        try {
            value = localcache.get(key);
            if("null".equals(value)){
                return null;
            }
            return value;
        } catch (Exception e) {
            logger.error("localCache get error", e);
        }
        return null;
    }
}
